﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using SAC.Elink;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string GetPlantPowerx(string timeStr, string planCode, string powerCode)
    {
        //string ConnType = ConfigurationManager.AppSettings["method"].ToString();
        //string ConnStr = ConfigurationManager.AppSettings["access"].ToString();
        //TargetGetAndSet targetOpera = new TargetGetAndSet(ConnType, ConnStr);
        //DateTime timeNow = DateTime.Now.AddMinutes(-5);
        //DateTime timeToday = timeNow.Date;
        //DateTime timePass = DateTime.Parse(timeStr);
        //int todayCount = timeNow.Hour * 12 + timeNow.Minute / 5 + 1;
        //if (timePass.Date < DateTime.Now.Date)
        //{
        //    timeNow = timePass.AddDays(1).AddSeconds(-1);
        //    timeToday = timeNow.Date;
        //    todayCount = 288;
        //}
        string plan = "[", today = "[", yesterday = "[";
        //double[] planArr = new double[289];
        //double[] todayArr = new double[todayCount];
        //double[] yestArr = new double[289];
        //for (int i = 0; i < 288; i++)
        //{
        //    TargetValue planValue = new TargetValue(planCode, timeToday);
        //    targetOpera.GetTargetValue(planValue);
        //    planArr[i] = planValue.DValue;
        //    plan += planValue.DValue.ToString("F2") + ",";
        //    if (i < todayCount)
        //    {
        //        TargetValue todayValue = new TargetValue(powerCode, timeToday);
        //        targetOpera.GetTargetValue(todayValue);
        //        todayArr[i] = todayValue.DValue;
        //        today += todayValue.DValue.ToString("F2") + ",";
        //    }

        //    TargetValue yestValue = new TargetValue(powerCode, timeToday.AddDays(-1));
        //    targetOpera.GetTargetValue(yestValue);
        //    yestArr[i] = yestValue.DValue;
        //    yesterday += yestValue.DValue.ToString("F2") + ",";
        //    timeToday = timeToday.AddMinutes(5);
        //}
        //yesterday += todayArr[0].ToString("F2") + "]";
        //plan += planArr[287].ToString("F2") + "]";
        //if (timePass.Date < DateTime.Now.Date)
        //{
        //    today += todayArr[287].ToString("F2") + "]";
        //}
        //else
        //{
        //    today = today.Substring(0, today.Length - 1) + "]";
        //}

        return "{\"plan\":" + plan + ",\"today\":" + today + ",\"yesterday\":" + yesterday + "}";
    }


    [WebMethod]
    public string GetFHByTime(string timeStr)
    {
        int ret = 0;
        int count = 288;
        double val1 = 0;
        double val2 = 0;
        double val0 = 0;

        double valj1 = 0;
        double valj2 = 0;
        double valj0 = 0;

        string fh1 = "WHSIS.U1APSH.U1A04013";
        string fh2 = "WHSIS.U2APSH.U2A04013";

        string zr1 = "[", zr2 = "[", zr0 = "[";
        string jr1 = "[", jr2 = "[", jr0 = "[";

        //string zr1 = "[", zr2 = "[", zr0 = "[";
        //string jr1 = "[", jr2 = "[", jr0 = "[";


        Elink ek = new Elink();

        string dtZr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).AddDays(-1).ToString();
        string dtJr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).ToString();

        DateTime dtNow = DateTime.Now;

        for (int i = 0; i <= count; i++)
        {
            ek.GetHisValue(fh1, dtZr, ref ret, ref val1);
            ek.GetHisValue(fh2, dtZr, ref ret, ref val2);
            val0 = val1 + val2;

            zr1 += val1.ToString("f2") + ",";
            zr2 += val2.ToString("f2") + ",";
            zr0 += val0.ToString("f2") + ",";


            if (DateTime.Parse(dtJr).AddMinutes(-5) < dtNow)
            {
                ek.GetHisValue(fh1, dtJr, ref ret, ref valj1);
                ek.GetHisValue(fh2, dtJr, ref ret, ref valj2);
                valj0 = valj1 + valj2;

                jr1 += valj1.ToString("f2") + ",";
                jr2 += valj2.ToString("f2") + ",";
                jr0 += valj0.ToString("f2") + ",";
            }

            dtZr = DateTime.Parse(dtZr).AddMinutes(+5).ToString();
            dtJr = DateTime.Parse(dtJr).AddMinutes(+5).ToString();
        }

        zr1 = zr1.TrimEnd(',') + "]";
        zr2 = zr2.TrimEnd(',') + "]";
        zr0 = zr0.TrimEnd(',') + "]";

        jr1 = jr1.TrimEnd(',') + "]";
        jr2 = jr2.TrimEnd(',') + "]";
        jr0 = jr0.TrimEnd(',') + "]";

        //zr1 = zr1.TrimEnd(',') + "]";
        //zr2 = zr2.TrimEnd(',') + "]";
        //zr0 = zr0.TrimEnd(',') + "]";
        
        //jr1 = jr1.TrimEnd(',') + "]";
        //jr2 = jr2.TrimEnd(',') + "]";
        //jr0 = jr0.TrimEnd(',') + "]";

        return "{\"zr1\":" + zr1 + ",\"zr2\":" + zr2 + ",\"zr0\":" + zr0 + ",\"jr1\":" + jr1 + ",\"jr2\":" + jr2 + ",\"jr0\":" + jr0 + "}";
    }

    [WebMethod]
    public string GetFHByTimeByFourMin(string timeStr)
    {
        int ret = 0;
        int count = 144;
        double val1 = 0;
        double val2 = 0;
        double val0 = 0;

        double valj1 = 0;
        double valj2 = 0;
        double valj0 = 0;

        string fh1 = "WHSIS.U1APSH.U1A04013";
        string fh2 = "WHSIS.U2APSH.U2A04013";

        string zr1 = "[", zr2 = "[", zr0 = "[";
        string jr1 = "[", jr2 = "[", jr0 = "[";

        //string zr1 = "[", zr2 = "[", zr0 = "[";
        //string jr1 = "[", jr2 = "[", jr0 = "[";


        Elink ek = new Elink();

        string dtZr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).AddDays(-1).ToString();
        string dtJr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).ToString();

        DateTime dtNow = DateTime.Now;

        for (int i = 0; i <= count; i++)
        {
            ek.GetHisValue(fh1, dtZr, ref ret, ref val1);
            ek.GetHisValue(fh2, dtZr, ref ret, ref val2);
            val0 = val1 + val2;

            zr1 += val1.ToString("f2") + ",";
            zr2 += val2.ToString("f2") + ",";
            zr0 += val0.ToString("f2") + ",";


            if (DateTime.Parse(dtJr).AddMinutes(-10) < dtNow)
            {
                ek.GetHisValue(fh1, dtJr, ref ret, ref valj1);
                ek.GetHisValue(fh2, dtJr, ref ret, ref valj2);
                valj0 = valj1 + valj2;

                jr1 += valj1.ToString("f2") + ",";
                jr2 += valj2.ToString("f2") + ",";
                jr0 += valj0.ToString("f2") + ",";
            }

            dtZr = DateTime.Parse(dtZr).AddMinutes(+10).ToString();
            dtJr = DateTime.Parse(dtJr).AddMinutes(+10).ToString();
        }

        zr1 = zr1.TrimEnd(',') + "]";
        zr2 = zr2.TrimEnd(',') + "]";
        zr0 = zr0.TrimEnd(',') + "]";

        jr1 = jr1.TrimEnd(',') + "]";
        jr2 = jr2.TrimEnd(',') + "]";
        jr0 = jr0.TrimEnd(',') + "]";

        //zr1 = zr1.TrimEnd(',') + "]";
        //zr2 = zr2.TrimEnd(',') + "]";
        //zr0 = zr0.TrimEnd(',') + "]";

        //jr1 = jr1.TrimEnd(',') + "]";
        //jr2 = jr2.TrimEnd(',') + "]";
        //jr0 = jr0.TrimEnd(',') + "]";

        return "{\"zr1\":" + zr1 + ",\"zr2\":" + zr2 + ",\"zr0\":" + zr0 + ",\"jr1\":" + jr1 + ",\"jr2\":" + jr2 + ",\"jr0\":" + jr0 + "}";
    }
}

